package com.neuedu.dao;


import com.neuedu.dao.BaseDao;
import com.neuedu.entity.User;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

@Repository  //当包扫描的时候 注册为Bean
public class UserDao implements BaseDao<User> {

    @Resource
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<User> queryList(String sql, Object... args) {

       List<User> list = jdbcTemplate.query(sql, new RowMapper<User>() {
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                int userId = rs.getInt("user_id");
                String userName = rs.getString("user_name");
                String gender = rs.getString("gender");
                Timestamp birthday = rs.getTimestamp("birthday");
                int status = rs.getInt("status");
                User user = new User();
                user.setUserId(userId);
                user.setGender(gender);
                user.setUserName(userName);
                user.setBirthday(birthday);
                user.setStatus(status);
                return user;
            }
        }, args);

        return list;
    }

    @Override
    public User queryOne(String sql, Object... args) {
        User user = jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                int userId = rs.getInt("user_id");
                String userName = rs.getString("user_name");
                String gender = rs.getString("gender");
                Timestamp birthday = rs.getTimestamp("birthday");
                int status = rs.getInt("status");
                User user = new User();
                user.setUserId(userId);
                user.setGender(gender);
                user.setUserName(userName);
                user.setBirthday(birthday);
                user.setStatus(status);
                return user;
            }
        }, args);
        return user;
    }

    @Override
    public User queryOne(Integer id) {
        String sql ="select * from t_user where user_id = ?";
        User user = jdbcTemplate.queryForObject(sql, new RowMapper<User>() {
            public User mapRow(ResultSet rs, int rowNum) throws SQLException {
                int userId = rs.getInt("user_id");
                String userName = rs.getString("user_name");
                String gender = rs.getString("gender");
                Timestamp birthday = rs.getTimestamp("birthday");
                int status = rs.getInt("status");
                User user = new User();
                user.setUserId(userId);
                user.setGender(gender);
                user.setUserName(userName);
                user.setBirthday(birthday);
                user.setStatus(status);
                return user;
            }
        }, id);
        return user;
    }

    @Override
    public Integer count(String sql, Object[] args) {
        return jdbcTemplate.queryForObject(sql,Integer.class,args);
    }

    @Override
    public boolean update(String sql, Object[] args) {
        return jdbcTemplate.update(sql,args) > 0;
    }
}
